#include "lerningx_in_yminutes.h"
#include "qrcutil.h"

LerningX_IN_YMinutes::LerningX_IN_YMinutes()
{

}

QString LerningX_IN_YMinutes::name()
{
    return "Leaning x in y minutes";
}

QString LerningX_IN_YMinutes::prefix()
{
    return "https://learnxinyminutes.com";
}

QList<QListWidgetItem *> LerningX_IN_YMinutes::searching_item(const QString &text)
{
    QList<QListWidgetItem *> list;

    QString first;;
    foreach (auto line, QR("://lerning-x-in-y-minutes.txt").split("\n", Qt::SkipEmptyParts)) {
        auto href = line.mid(line.indexOf("\"/")+1);
        href = href.mid(0, href.indexOf("\""));

        auto title = line.mid(line.indexOf(">")+1);
        title = title.mid(0, title.indexOf("<"));

        if (text.isEmpty() || href.contains(text, Qt::CaseInsensitive)) {
            auto item = newItem();
            if (href.split("/", Qt::SkipEmptyParts).count() == 3) {
                item->setText( first + "["+title+"]");
            } else {
                first = title;
                item->setText(title);
            }
            item->setData(990, href);
            list << item;
        }
    };

    return list;
}

QString LerningX_IN_YMinutes::content_item(const QListWidgetItem *item)
{
    return prefix() + item->data(990).toString();
}
